package com.juvepoland.jpdroid.managers.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.juvepoland.jpdroid.common.JSONParser;
import com.juvepoland.jpdroid.models.Comment;
import com.juvepoland.jpdroid.models.News;
import com.juvepoland.jpdroid.models.User;

public class CommentManager {
	
	static final String URL_GET_COMMENTS 			= "http://jpdroid.juvepoland.com/get_all_comments.php";
	static final String URL_GET_COMMENT_DETAILS		= "http://jpdroid.juvepoland.com/get_comment_details.php";
	static final String URL_SAVE_COMMENT			= "http://jpdroid.juvepoland.com/create_comment.php";
	
	
	List<Comment> commentList = new ArrayList<Comment>();
	
	private int currentIndex = -1;
	
	
	public List<Comment> getCommentsList(int news_id, int index, int limit) throws Exception{
		
		if(index == currentIndex){
			return commentList;
		}
		currentIndex = index;
		
		JSONParser jParser = new JSONParser();
		JSONArray jArray = new JSONArray();
		JSONObject jObject = null;
			
		
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("news_id", String.valueOf(news_id)));
        params.add(new BasicNameValuePair("index", String.valueOf(index)));
        params.add(new BasicNameValuePair("limit", String.valueOf(limit)));
        
        JSONObject jsonObject = jParser.makeHttpRequest(URL_GET_COMMENTS, "GET", params);
        int success = jsonObject.getInt("success");
        if (success == 1) {
        	JSONArray commentsArray = jsonObject.getJSONArray("comments"); // JSON Array
        	Comment comment = null;
        	for (int i = 0; i < commentsArray.length(); i++) {
        		JSONObject c = commentsArray.getJSONObject(i);
        		commentList.add(getCommentFromJSONObject(c));
        	}
        	
        }
		return commentList;
	}
	
	private void setCommentDate(Comment comment, JSONObject c) throws JSONException{
		Date date = new Date();
		long timestamp = (long)c.getInt(Comment.D_DATE);
		timestamp = timestamp*1000;
		date.setTime(timestamp);
		comment.setDate(date);
	}
	
	private Comment getCommentFromJSONObject(JSONObject jsonObject) throws Exception{
		Comment comment = new Comment();
		comment.setId(jsonObject.getInt(Comment.D_ID));
		comment.setContent(jsonObject.getString(Comment.D_CONTENT));
		comment.setDate(jsonObject.getInt(Comment.D_DATE));
		//retrieve comment's user:
		User user = new User();		//TODO 	WSTAWIĆ METODĘ
		user.setId(jsonObject.getInt("uid"));
		user.setAvatarSource(jsonObject.getString("user_avatar"));
		user.setUsername(jsonObject.getString("username"));
		comment.setUser(user);
		return comment;
	}
	
	public Comment getComment(int id){
		JSONParser jParser = new JSONParser();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("cid", String.valueOf(id)));
		JSONObject json = jParser.makeHttpRequest(URL_GET_COMMENT_DETAILS, "GET", params);
		int success = 0;
		try {
			success = json.getInt("success");
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			Log.d("cos sie spierdolilo ", "");
		}
		if (success == 1) {
			try{
				JSONArray productObj = json.getJSONArray("comment");
				JSONObject c = productObj.getJSONObject(0);
				
				return getCommentFromJSONObject(c);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
		
	}

	public void saveComment(Comment comment) {
		JSONParser jParser = new JSONParser();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		 params.add(new BasicNameValuePair("content", comment.getContent()));
		 params.add(new BasicNameValuePair("unixtime", comment.getDateLong()+""));
		 params.add(new BasicNameValuePair("user_id", comment.getUser().getId()+""));
		 params.add(new BasicNameValuePair("news_id", comment.getNews().getId()+""));
		 params.add(new BasicNameValuePair("gid", comment.getNewsType().getCode()));
		 JSONObject json = jParser.makeHttpRequest(URL_SAVE_COMMENT, "POST", params);

	}

}
